#!/bin/bash

main(){
    echo "-----------------"
    echo "Please choose:"
    echo "1 ) Install"
    echo "2 ) Uninstall"
    echo "q ) Quit"
    echo "Type word and press enter:"
    read x
    case $x in
        1 )
            echo "Installing..."
            dirs="/opt/ups_main_ship/"
			if [ ! -d "$dirs" ];then
				 mkdir -p $dirs
				 echo "创建目录:$dirs成功"
			else
				echo "目录:$dirs 已经存在"
			fi
			cp -apf config  $dirs
			cp -apf main_ship $dirs
			chmod +x $dirs/main_ship

		    sed -i '/ups_main_ship_service/d' /etc/crontab
			cp -apf ups_main_ship_service  /sbin/ups_main_ship_service
			chmod +x /sbin/ups_main_ship_service
			source /etc/profile

			echo "" >> /etc/crontab
			echo "@reboot root /sbin/ups_main_ship_service > /dev/null" >> /etc/crontab
			systemctl restart crond.service
			killall main_ship > /dev/null
			#start main_ship
			/sbin/ups_main_ship_service start
			echo "安装目录在：$dirs"
			echo "通过 ups_main_ship_service 运行(start,stop,restart)"
			echo "Install complete!"
			exit

            ;;
        2 )
            echo "Uninstalling..."
			rm  -rf /opt/ups_main_ship
            rm -rf /sbin/ups_main_ship_service -rf
            sed -i '/ups_main_ship_service/d' /etc/crontab
            systemctl restart crond.service
            killall main_ship > /dev/null
            echo "Uninstall complete!"
            exit
            ;;
        q )
            echo "Now quit."
            ;;
        * )
            echo "Please input right word."
            main
    esac
}

main
